Skip to content

Conversation

ABCDeath
Copy link
Contributor

I've found an issue in Connection.copy_to_table() method with a path-like source argument. The following code throws exception io.UnsupportedOperation: read:

import asyncio
import asyncpg

async def main():
    conn = await asyncpg.connect(connection_string)
    await conn.execute('CREATE TABLE test_table(a text, "b~" text, i int);')
    await conn.copy_to_table('test_table', source=filename)
    
filename = 'table_content'
f = open(filename, 'wb')
f.write(
    '\n'.join([
        'a1\tb1\t1',
        'a2\tb2\t2',
        'a3\tb3\t3',
        'a4\tb4\t4',
        'a5\tb5\t5',
        ''
    ]).encode('utf-8')
)
f.close()

asyncio.run(main())

It looks like Connection._copy_in() method opens file in a wrong mode (wb instead of rb).
So this fix:

  • Change file opening mode to rb;
  • Add test case for Connection.copy_to_table() method with a path-like source.

 * Change file opening mode to 'rb'
 * Add test case for Connection.copy_to_table with path-like source
Copy link
Member

@elprans elprans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants